package cn.pfz.Service.impl;

import cn.pfz.Service.IDepartmentSerives;
import cn.pfz.domain.Department;
import cn.pfz.domain.Employee;
import cn.pfz.mapper.DepartmentMapper;
import cn.pfz.query.QueryObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IDepartmentSerivesimpl implements IDepartmentSerives {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public int deleteByPrimaryKey(Long id) {
        return departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Department record) {
        return departmentMapper.insert(record);
    }

    @Override
    public Department selectByPrimaryKey(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> selectAll() {
        return departmentMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(Department record) {
        return departmentMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo<Department> list(QueryObject qo) {
        //通过pageHelper进行设置分页及排序
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //在Sql中不需要limit
        List<Department> departments = departmentMapper.selectRusult(qo);
        PageInfo<Department> PageInfo = new PageInfo(departments);
        return PageInfo;

    }

    @Override
    public Workbook getWrokbook() {
        //创建一个excel表格
        Workbook workbook = new HSSFWorkbook();

        //创建工作区
        Sheet st = workbook.createSheet("部门表");
        Row row = st.createRow(0);
        row.createCell(0).setCellValue("编号");
        row.createCell(1).setCellValue("部门名");
        row.createCell(2).setCellValue("简写");
        //获取要导出的数据
        List<Department> departments = departmentMapper.selectAll();
        //循环写入表格
        for (int i = 0; i < departments.size(); i++) {

            Department department = departments.get(i);
            Row rows = st.createRow(i+1);
            rows.createCell(0).setCellValue(i+1);
            if (department.getName()!=null){
                rows.createCell(1).setCellValue(department.getName());
            }else{
                rows.createCell(1).setCellValue("");
            }
            if (department.getSn()!=null){
                rows.createCell(2).setCellValue(department.getSn());
            }else{
                rows.createCell(2).setCellValue("");
            }
        }

        //将表格返回
        return workbook;
    }
}
